
Imports std
Imports storoomc
Imports storcore

Public Module common

    Private Enum stornodeTypeEnum
        stornode
        stornode_redundance2
        stornode_redundance3
    End Enum

    Public Sub initialStornode(ByVal node As stornode, ByVal machine As String)
        assert(Not node Is Nothing, "node is nothing.")
        node.maxTalkCount = config.parameter(configNaming.storoom.name _
                                    , configNaming.storoom.machineTalkCount + machine _
                                    , configNaming.storoom.defaultValue.machineTalkCount)
        If node.maxTalkCount <= 0 Then
            node.maxTalkCount = configNaming.storoom.defaultValue.machineTalkCount
            raiseError("caught an invalid configuration in [" + configNaming.storoom.name _
                       + "]\" + configNaming.storoom.machineTalkCount + machine _
                       , errorHandle.errorType.warning)
        End If
        node.holdTime = config.parameter(configNaming.storoom.name _
                                          , configNaming.storoom.talkHoldtime _
                                          , configNaming.storoom.defaultValue.talkHoldtime)
        If node.holdTime <= 0 Then
            node.holdTime = configNaming.storoom.defaultValue.talkHoldtime
            raiseError("caught an invalid configuration in [" + configNaming.storoom.name _
                        + "]\" + configNaming.storoom.talkHoldtime, errorHandle.errorType.warning)
        End If
        node.liveTime = config.parameter(configNaming.storoom.name _
                                          , configNaming.storoom.talkLivetime _
                                          , configNaming.storoom.defaultValue.talkLivetime)
        If node.liveTime <= 0 Then
            node.liveTime = configNaming.storoom.defaultValue.talkLivetime
            raiseError("caught an invalid configuration in [" + configNaming.storoom.name _
                        + "]\" + configNaming.storoom.talkLivetime, errorHandle.errorType.warning)
        End If
        node.retrytimes = config.parameter(configNaming.storoom.name _
                                            , configNaming.storoom.retrytimes _
                                            , configNaming.storoom.defaultValue.retrytimes)
        If node.retrytimes = 0 Then
            node.retrytimes = configNaming.storoom.defaultValue.retrytimes
            raiseError("caught an invalid configuration in [" + configNaming.storoom.name _
                        + "]\" + configNaming.storoom.retrytimes, errorHandle.errorType.warning)
        End If

        node.initial()
    End Sub

    Public Sub nodesStart(ByVal slicesmgr As storoomOper(Of storoomDistrTM))
        slicesmgr.slices = New vector(Of storageInterface)
        Dim machinelist As String = Nothing
        machinelist = config.parameter(configNaming.storoom.name, _
                                       configNaming.storoom.machinelist, _
                                       configNaming.storoom.defaultValue.machinelist)

        Dim stornodeType As stornodeTypeEnum = Nothing
        Dim stornodeTypeStr As String = Nothing
        stornodeTypeStr = config.parameter(configNaming.storoom.name, configNaming.storoom.stornodeType, _
                                           configNaming.storoom.defaultValue.stornodeType)
        If strsame(stornodeTypeStr, "stornode_redundance2", False) Then
            stornodeType = stornodeTypeEnum.stornode_redundance2
        ElseIf strsame(stornodeTypeStr, "stornode_redundance3", False) Then
            stornodeType = stornodeTypeEnum.stornode_redundance3
        Else
            stornodeType = stornodeTypeEnum.stornode
        End If

        'nodes_hash is order-sensitive
        Dim machines As vector(Of String) = Nothing
        strsepar(machinelist, character.comma, machines, True)
        If Not machines Is Nothing AndAlso machines.size() > 0 Then
            If strsepar(machinelist, character.comma, New [set](Of String), True).size() <> machines.size() Then
                raiseError("duplcate machine found in machine list " + machinelist, errorHandle.errorType.warning)
            End If

            Dim i As Int64
            For i = 0 To machines.size() - 1
                Dim address As String = Nothing
                Dim port As String
                address = config.parameter(Of String)(configNaming.storoom.name _
                                                        , configNaming.storoom.machineaddress + machines(i) _
                                                        , configNaming.storoom.defaultValue.machineaddress)
                port = config.parameter(configNaming.storoom.name _
                                        , configNaming.storoom.machineport + machines(i) _
                                        , configNaming.storoom.defaultValue.machineport)

                Dim node As stornodebase = Nothing
                'stornode_redundance2 will initial itself
                If stornodeType = stornodeTypeEnum.stornode Then
                    node = New stornode(address, port)
                    initialStornode(node, machines(i))
                ElseIf stornodeType = stornodeTypeEnum.stornode_redundance2 Then
                    node = New stornode_redundance2(address, port)
                ElseIf stornodeType = stornodeTypeEnum.stornode_redundance3 Then
                    node = New stornode_redundance3(address, port)
                Else
                    assert(False, "should cover all situations.")
                End If
                assert(slicesmgr.slices.push_back(node), "slices.push_back(node) returns false.")
            Next
        End If

        slicesmgr.enablecache = config.parameter(configNaming.storoom.name _
                                                   , configNaming.storoom.enablecache _
                                                   , configNaming.storoom.defaultValue.enablecache)
        slicesmgr.distributeOperationHoldtime = config.parameter(configNaming.storoom.name _
                                                        , configNaming.storoom.distributeOperationHoldtime _
                                                        , configNaming.storoom.defaultValue.distributeOperationHoldtime)
        slicesmgr.threadCount = config.parameter(configNaming.storoom.name, _
                                                 configNaming.storoom.threadcount, _
                                                 configNaming.storoom.defaultValue.threadcount)
        raiseError("finish nodesStart, done initial all the slices.")
    End Sub

    Private Function showsliceImpl(ByVal slice As stornodebase) As String
        assert(Not slice Is Nothing, "slice is nothing.")
        Return slice.display()
    End Function

    Public Function showslice(ByVal slice As storageInterface) As String
        Return showsliceImpl(convertor.cast(Of stornodebase)(slice))
    End Function
End Module
